Repository navigation
String.match returns string[] | null; lib.ts compiles under strict null - #98
Merged
Merged
Conversation
match returned null through a result declared string[]. It is declared string[] | null now, in lib.ts and lib.d.ts, and a global match that finds nothing returns null too, as in JavaScript, where it returned an empty array. A caller that used the result without a null check no longer compiles: string_match.ts and string_match_lastIndex.ts check for null first. With that, `// @strict-null false` is gone from lib.ts. The compiler then rejected the null passed to three C functions, whose parameters are declared nullable in lib.native.d.ts: the end pointer of strtol and strtod, and the results argument (and the result) of regexp_exec. -mm=own: match puts `[...result]` in a local before returning it; returned directly as string[] | null it was "a second reference". Tested in release on Windows x64 for gc, rc, none and own, compiled and under the JIT; debug, Linux, x86 and wasm were not built locally. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The commit before made it null, as JavaScript does. It goes back to the empty array it always was here, so a global match can be walked or counted without a check; only a pattern without the g flag gives null. string_match.ts asserts both. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The last two items left out of #96.
What
String.matchreturnsstring[] | null, inlib.ts(the__Stringfunction and theStringclass method) and inlib.d.ts. It returnednullthrough a result declaredstring[].// @strict-null falseis removed fromlib.ts, so the library itself now compiles under the strict null check.Behaviour
matchreturns at run time is unchanged:nullwhen a pattern without thegflag does not match, and an empty array when a global one finds nothing. That second case differs from JavaScript, which givesnullthere too; it is kept so a global match can always be walked or counted.<string>foundorfound.length).if (match) { ... }still compiles unchanged.What the stricter compile needed
nullpassed to three C functions. Their parameters are now declared nullable inlib.native.d.ts: the end pointer ofstrtolandstrtod, and the results argument and the result ofregexp_exec.-mm=ownrejectedreturn [...result]with the new return type ("a second reference"), somatchputs the array in a local first, asmatchAlldoes before itsyield.The
searchString == nullguards in the string helpers stay: they compile under strict null and still catch a null string from a caller.Testing
tests/string_match.tsandtests/string_match_lastIndex.tscheck for null before using the result, andstring_match.tsasserts null for no match withoutgand an empty array with it.scripts/check_declarations.ps1passes.lib.linux.tsandlib.wasm.tsare now compiled under strict null as well; the only literalnullin them is in a declaration that is already nullable.🤖 Generated with Claude Code